package zhengdaoyang.settings.dao;

import zhengdaoyang.settings.domain.Goods;
import zhengdaoyang.settings.domain.User;
import zhengdaoyang.utils.MyDbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDaoImpl implements GoodDao {

    /*查找商品*/
    @Override
    public List<Goods> getGoodDate(String name) {

        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "select * from goods where gname=?";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        List<Goods> goodsList = new ArrayList<Goods>();
        try {
            rs =  ps.executeQuery();
           while(rs.next()){
              Goods goods = new Goods();
               int  gid = rs.getInt("gid");
               String gname = rs.getString("gname");
               String gimgaddress  =rs.getString("gimgaddress");
               double gprice = rs.getDouble("gprice");
               String gdescription = rs.getString("gdescription");

               goods.setGid(gid);
               goods.setGname(gname);
               goods.setGdescription(gdescription);
               goods.setGimgaddress(gimgaddress);
               goods.setGprice(gprice);
               goodsList.add(goods);
           }
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕="+goodsList);
        return goodsList;
    }

    /*查找  分类下 全部商品*/
    @Override
    public List<Goods> findallgoods(String name) {

        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "select * from goods where gname like ? ";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        List<Goods> goodsList = new ArrayList<Goods>();
        try {
            rs =  ps.executeQuery();
            while(rs.next()){
                Goods goods = new Goods();
                int  gid = rs.getInt("gid");
                String gname = rs.getString("gname");
                String gimgaddress  =rs.getString("gimgaddress");
                double gprice = rs.getDouble("gprice");
                String gdescription = rs.getString("gdescription");

                goods.setGid(gid);
                goods.setGname(gname);
                goods.setGdescription(gdescription);
                goods.setGimgaddress(gimgaddress);
                goods.setGprice(gprice);
                goodsList.add(goods);
            }
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕="+goodsList);
        return goodsList;
    }

    @Override
    public int insertcart(String name,double price) {

        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "INSERT into cart(bname,bnumber,bprice) VALUES(?,1,?)";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            ps.setDouble(2,price);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs=null;
        int i=0;
        try {
            i= ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=" + i);

        return i;
    }
}
